package com.situ.day13;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import org.junit.Test;

public class testFile {
	@Test
	public void testFileReader() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			fileReader.read();
			System.out.println(fileReader.read());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test1() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println((char) (result));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test2() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println(result);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test3() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println((char) (result));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test4() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println((char) (result));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test5() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println((char) (result));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test6() {
		try {
			FileReader fileReader = new FileReader("a.txt");
			int result = fileReader.read();
			System.out.println((char) (result));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test7() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a.txt");
			while (fileReader.read() != -1) {
				System.out.println((char) (fileReader.read()));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void test8() {
		try {
			FileReader fileReader = new FileReader("./src/2.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	public void test9() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a.txt");
			int ch = -1;
			while ((ch = fileReader.read()) != -1) {
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	@Test
	public void test10() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("a.txt");
			fileWriter = new FileWriter("a_back.txt");
			int length = 0;
			char[] buffer = new char[10];
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(Arrays.toString(buffer));
				fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void test11() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("tupian.jpg");
			fileOutputStream = new FileOutputStream("tupian_block.jpg");
			int length = 0;
			byte[] buffer = new byte[1024];
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void test12() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			 fileReader = new FileReader("a.txt");
			 fileWriter = new FileWriter("a_block.txt");
			 char[] buffer = new char[10];
			 int length = 0;
			 while ((length = fileReader.read(buffer)) != -1) {
				 System.out.println(length);
				 System.out.println(Arrays.toString(buffer));
				 fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void test13() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			 fileInputStream = new FileInputStream("src/tupian.jpg");
			 fileOutputStream = new FileOutputStream("tupianxin.jpg");
			 byte[] buffer= new byte[1024];
			 int length = 0;
			 while ((length = fileInputStream.read(buffer)) != -1) {
				 fileOutputStream.write(buffer, 0, length);
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void test14() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("a.txt");
			fileWriter = new FileWriter("a_bnsd.txt");
			int length = 0;
			char[] buffer = new char[10];
			while ((length = fileReader.read(buffer)) != -1) {
				fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

}
